package com.example.constellation.mefrag;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.example.constellation.R;
import com.example.constellation.bean.StarBean;
import com.example.constellation.luckfrag.LuckBaseAdapter;
import com.example.constellation.utils.AssetsUtils;

import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

public class MeFragment extends Fragment implements View.OnClickListener {

    CircleImageView iconIv;
    TextView nameTv;
    private Map<String, Bitmap> imageMap;
    private List<StarBean.StarinfoBean> mDatas;
    private SharedPreferences star_pref;
    //    保存选择的星座位置(在GridView当中)
    int selectPos = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        获取activity传递的数据
        Bundle bundle = getArguments();
        StarBean infoBean = (StarBean) bundle.getSerializable("info");
        mDatas = infoBean.getStarinfo();
//        共享参数（利用 SharedPreference 存储数据）
        star_pref = getContext().getSharedPreferences("star_pref", Context.MODE_PRIVATE);//私有

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_me, container, false);
        iconIv = view.findViewById(R.id.mefrag_iv);
        nameTv = view.findViewById(R.id.mefrag_tv_name);
        iconIv.setOnClickListener(this);
//        进行初始化设置
        imageMap = AssetsUtils.getContentlogoImgMap();
//        读取共享参数当中保存的星座名称和logo名称
        String name = star_pref.getString("name", "白羊座");//参数2：默认值
        String logoname = star_pref.getString("logoname", "baiyang");
//        依次设置
        Bitmap bitmap = imageMap.get(logoname);
        iconIv.setImageBitmap(bitmap);
        nameTv.setText(name);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mefrag_iv:
                showDialog();
                break;
        }
    }

    private void showDialog() {
//        创建对话框
        Dialog dialog = new Dialog(getContext());
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.me_dialog,null);
//        传入要加载的View
        dialog.setContentView(dialogView);
        dialog.setTitle("请选择您的星座：");
        GridView dialogGv = dialogView.findViewById(R.id.mefrag_dialog_gv);
//        设置适配器(复用LuckBaseAdapter)
        LuckBaseAdapter adapter = new LuckBaseAdapter(getContext(),mDatas);
        dialogGv.setAdapter(adapter);
//          设置能被取消
        dialog.setCancelable(true);
//        设置点击外部能够取消弹出框
        dialog.setCanceledOnTouchOutside(true);

//        设置GridView每一项的点击事件
        dialogGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                StarBean.StarinfoBean bean = mDatas.get(position);
                String name = bean.getName();
                String logoname = bean.getLogoname();
                nameTv.setText(name);
                Bitmap bitmap = imageMap.get(logoname);
                iconIv.setImageBitmap(bitmap);
                selectPos = position;   //保存选择的位置
                dialog.cancel();
            }
        });
        dialog.show();
    }
//  失去焦点
    @Override
    public void onPause() {
        super.onPause();
        StarBean.StarinfoBean bean = mDatas.get(selectPos);
        String name = bean.getName();
        String logoname = bean.getLogoname();
//        将取出来的 name 以及 logoname 存放到 SharedPreference 当中
        SharedPreferences.Editor editor = star_pref.edit();//获取共享参数中写入数据的对象
        editor.putString("name",name);
        editor.putString("logoname",logoname);
//        提交
        editor.commit();

    }
}